package chuangyi.com.org.DOMIHome;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;


import com.umeng.socialize.UMShareAPI;

import java.security.InvalidParameterException;

import chuangyi.com.org.ViewModel.ContextObservable;
import chuangyi.com.org.ViewModel.PayModel;

/**
 * 启动时需要传入layoutId参数
 * Created by zxh on 16/11/17.
 */
public class PopupActivity extends AppCompatActivity {
    public final static int REQUEST_PHOTO = 1;
    public final static int REQUEST_CAMERA = 2;

    private static ContextObservable dataModel;

    public static void setDataModel(ContextObservable data) {
        if (dataModel != null) {
            throw new InvalidParameterException("当前窗口正在使用");
        }
        dataModel = data;
    }

    public static void notifyPayResult(Boolean success) {
        if (dataModel instanceof PayModel) {
            PayModel model = (PayModel) dataModel;
            model.notifyPayResult(success);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 去除dialog边距
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        int layoutId = bundle.getInt("layoutId");
        int variableId = bundle.getInt("variableId");
        ViewDataBinding dataBinding = DataBindingUtil.setContentView(this, layoutId);

        if (dataModel != null) {
            dataModel.setContext(this);
        }
        dataBinding.setVariable(variableId, dataModel);
    }

//    //实现onTouchEvent触屏函数但点击屏幕时销毁本Activity
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        finish();
//        return true;
//    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void finish() {
        dataModel = null;
        super.finish();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        ContextObservable contextObservable = dataModel;
        if (contextObservable != null && contextObservable instanceof WaitActivityResult) {
            WaitActivityResult waitActivityResult = (WaitActivityResult) contextObservable;
            waitActivityResult.onActivityResult(requestCode, resultCode, data);
        }
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    public static void startActivity(@NonNull Activity activity, @LayoutRes int layoutId, @Nullable Integer variableId, @Nullable ContextObservable model) {
        Intent intent = new Intent();
        intent.setClass(activity, PopupActivity.class);
        intent.putExtra("layoutId", layoutId);
        intent.putExtra("variableId", variableId);
        PopupActivity.setDataModel(model);
        activity.startActivity(intent);
    }

    public static void startActivityForResult(@NonNull Activity activity, @LayoutRes int layoutId, int requestCode, @Nullable Integer variableId, @Nullable ContextObservable model) {
        Intent intent = new Intent();
        intent.setClass(activity, PopupActivity.class);
        PopupActivity.setDataModel(model);
        intent.putExtra("layoutId", layoutId);
        intent.putExtra("variableId", variableId);
        activity.startActivityForResult(intent, requestCode);
    }
}
